package com.weijianhuawen.wjblog.admin.event.subscriber;

import com.weijianhuawen.wjblog.admin.event.DeleteArticleEvent;
import com.weijianhuawen.wjblog.search.helper.LuceneHelper;
import com.weijianhuawen.wjblog.search.index.ArticleIndex;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.index.Term;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-05 22:05
 */
@Component
@Slf4j
public class DeleteArticleSubscriber implements ApplicationListener<DeleteArticleEvent> {
    @Autowired
    private LuceneHelper luceneHelper;
    @Override
    public void onApplicationEvent(DeleteArticleEvent event) {
        // 消费 删除文章 事件
        Long articleId = event.getArticleId();
        // 获取当前线程名称
        String threadName = Thread.currentThread().getName();

        log.info("【删除文章事件订阅者】 线程：{}开始处理订阅者行为！", threadName );
        log.info("【删除发布事件订阅者】 开始消费！处理文章id：{}", articleId);
        // 根据id删除对应的文档
        Term term = new Term(ArticleIndex.COLUMN_ID, String.valueOf(articleId));
        Long count = luceneHelper.deleteDocument(ArticleIndex.NAME, term);
        log.info("【删除发布事件订阅者】 完成！受影响文档数：{}", count);
    }
}
